﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace DungeonDelver.Engine
{
    public class Map : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new Map(theParent as AtlasCell, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Map(theParent as AtlasCell);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.MapFactory, new Map.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.MapFactory);
            }
        }

        private Map(AtlasCell parent)
            : base(parent)
        {
        }

        private Map(AtlasCell parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        public AtlasCell AtlasCell
        {
            get
            {
                return ParentEntity as AtlasCell;
            }
        }
        public Game Game
        {
            get
            {
                return AtlasCell != null ? AtlasCell.Game : null;
            }
        }

        public int Layer
        {
            get
            {
                if (AtlasCell != null)
                {
                    foreach (var layer in AtlasCell.Layers)
                    {
                        if (AtlasCell[layer] == this)
                        {
                            return layer;
                        }
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }
        private Dictionary<int, MapColumn> maps = new Dictionary<int, MapColumn>();

        public Dictionary<int, MapColumn>.KeyCollection Columns
        {
            get
            {
                return maps.Keys;
            }
        }

        public MapColumn this[int index]
        {
            get
            {
                if (maps.ContainsKey(index))
                {
                    return maps[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (maps.ContainsKey(index))
                {
                    maps[index].ParentEntity = null;
                    maps.Remove(index);
                }
                if (value != null)
                {
                    maps[index] = value;
                }
            }
        }

        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.Map);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.MapFactory));
            if (AtlasCell != null)
            {
                result.Add(new XAttribute(Names.EngineXNames.Layer, Layer.ToString()));
            }
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            if (AtlasCell != null)
            {
                AtlasCell[int.Parse(theXElement.Attribute(Names.EngineXNames.Layer).Value)] = this;
            }
        }

        public static Map Create(AtlasCell theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.MapFactory) as Map;
        }

        public static Map Load(AtlasCell theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as Map;
        }
    }
}
